<template>
	<view class="container">
		<view class="main">
			<scroll-view
				class="list"
				scroll-y
				enable-flex
				@scrolltolower="handleScrollTolower"
			>
				<view class="list__item" :class="{ 'list__item--right': item.flow == 'OUT'  }" v-for="item in list">
					<view>
						<image 
							mode="aspectFill" 
							class="list__item--image"
							:src="getUserAvater(item)"
						/>
					</view>
					<view class="list__item__message">
						<text class="list__item__message--text">
							{{ item.message }}
						</text>
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="footer"></view>
	</view>
</template>

<script>
	import { MessageData } from '@/utils/utils.js'
	import Test1Image from '@/static/image/test1.jpg'
	import Test2Image from '@/static/image/test2.jpg'
	function randomString(length) {
	    var str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	    var result = '';
	    for (var i = length; i > 0; --i) 
	        result += str[Math.floor(Math.random() * str.length)];
	    return result;
	}
	export default {
		name: 'ScrollReversePage',
		onLoad() {
			this.list = MessageData.reverse()
		},
		data() {
			return {
				list: []
			};
		},
		computed: {
			getUserAvater() {
				return function(item) {
					return item.flow == 'OUT' ? Test1Image : Test2Image
				}
			}
		},
		methods: {
			// 触底
			handleScrollTolower() {
				uni.showLoading({
					title: '加载中...',
				})
				setTimeout(() => {
					for(let i = 0; i <= 10; i++) {
						const item = {
							flow: 'OUT',
							message: randomString(30)
						}
						this.list.push(item)
					}
					uni.hideLoading()
				}, 2000)
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		width: 100%;
		height: 100%;
		background-color: #f5f5f5;
	}
	.container {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100%;
	}
	.main {
		height: calc(100% - 100rpx);
		display: flex;
		flex-direction: column;
		/deep/.uni-scroll-view-content {
			display: flex;
			flex-direction: column;
		}
		.list {
			box-sizing: border-box;
			flex: 1;
			display: flex;
			flex-direction: column;
			overflow: scroll;
			padding: 0 32rpx;
			transform: rotateX(180deg);
			&__item {
				margin-top: 24rpx;
				display: flex;
				transform: rotateX(-180deg);
				&--right {
					flex-direction: row-reverse;
					margin-right: 64rpx;
				}
				&--image {
					width: 72rpx;
					height: 72rpx;
					border-radius: 100%;
				}
				&__message {
					display: flex;
					align-items: center;
					max-width: 64%;
					background-color: #5898F8;
					padding: 24rpx;
					margin: 0 24rpx;
					border-radius: 16rpx;
					color: #FFFFFF;
					overflow: hidden;
					&--text {
						word-break:break-all;
						font-size: 30rpx;
					}
				}
			}
		}
	}
	.footer {
		width: 100%;
		height: 100rpx;
		background-color: #FFFFFF;
	}
</style>
